﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WickedFlame.Core.Tools
{
    public class ColorPickerTool : VectorTool
    {
        Window baseWindow;

        public ColorPickerTool(Window baseWindow)
            : base()
        {
            this.baseWindow = baseWindow;
        }

        public override void OnMouseLeftButtonUp(DrawingCanvas sender, MouseButtonEventArgs e)
        {
            byte[] pixels = GetSnapshotColor();

            if (pixels != null)
                ShapePropertySettings.Settings.Fill = new SolidColorBrush(Color.FromArgb((byte)255, pixels[2], pixels[1], pixels[0]));
        }

        public override void OnMouseRightButtonUp(DrawingCanvas sender, MouseButtonEventArgs e)
        {
            byte[] pixels = GetSnapshotColor();

            if (pixels != null)
                ShapePropertySettings.Settings.Stroke = new SolidColorBrush(Color.FromArgb((byte)255, pixels[2], pixels[1], pixels[0]));
        }

        private byte[] GetSnapshotColor()
        {
            FrameworkElement element = null;
            byte[] pixels = null;

            if (baseWindow.IsMouseOver)
                element = baseWindow;
            foreach (FrameworkElement el in baseWindow.OwnedWindows)
            {
                if (el.IsMouseOver)
                {
                    element = el;
                    break;
                }
            }

            if (element != null)
            {
                // crate a renderbitmap with the correct size
                var renderBitmap = new RenderTargetBitmap(
                                                      (int)element.Width,
                                                      (int)element.Height,
                                                      96d,
                                                      96d,
                                                      PixelFormats.Pbgra32);
                // render the image
                renderBitmap.Render(element);

                var strm = new System.IO.MemoryStream();

                var encoder = new PngBitmapEncoder();
                encoder.Interlace = PngInterlaceOption.On;
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                encoder.Save(strm);
                strm.Seek(0, System.IO.SeekOrigin.Begin);

                var image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = strm;
                image.EndInit();

                //remove all references
                image.StreamSource.Dispose();

                try
                {
                    var cb = new CroppedBitmap(image,
                        new Int32Rect((int)Mouse.GetPosition(element).X,
                            (int)Mouse.GetPosition(element).Y, 1, 1));

                    pixels = new byte[4];

                    try
                    {
                        cb.CopyPixels(pixels, 4, 0);
                    }
                    catch { }
                }
                catch { }

            }

            return pixels;
        }

    }
}
